<?php
	defined('_JEXEC') or die('Restricted access');
   
	//
	// t_base_component
	//
//	abstract class t_base_component
	abstract class t_base_component implements i_base_component
	{
		// property declaration
		// public
        // D
        public $default_method = false;
        //I
        public $id = false;
        // M
		public $main_query_parameter = false;

		// P
		public $parent_cuteengine_component = false; // t_cuteengine_component
        public $parent_id = null;
        // U
        public $user_agent = false; //new t_user_agent_component;

		// method declaration
        // protected

        
        abstract protected function get_specific_parameters_position_last();
        // I
		abstract protected function initialize_query_parameters();
		// public 
        public function __construct( $a_cuteengine_component ) 
		{
			global $g_strings_utility_namespace;
            $this->parent_cuteengine_component = $a_cuteengine_component;
			$this->id = $this->get_main_query_parameter_value(t_cuteengine_component::main_query_component_id);
            //echo "nuovo id:$this->id <br>";
			$this->parent_id = $this->get_main_query_parameter_value(t_cuteengine_component::main_query_component_parent_id);
			//echo __METHOD__ ." - this->parent_id: " .$this->get_main_query_parameter_value(t_cuteengine_component::main_query_component_parent_id) ." <BR>"; 
            $this->main_query_parameter = $a_cuteengine_component->main_query_parameter;
            $this->initialize_query_parameters();
			$this->log = $this->parent_cuteengine_component->log;
            if (isset($g_ambient_namespace))
                $this->user_agent = $g_ambient_namespace->user_agent; 
            else 
                $this->user_agent = new t_user_agent_component;
            // specific_parameters
			//echo "a_part_specific_parameters: $a_part_specific_parameters<br>";
			//echo "main_query_field_name_specific_parameters: ".$this->main_query_parameter->get_parameter_value(
					//t_cuteengine_component::main_query_field_name_specific_parameters,-1). "<br>";
			$this->specific_parameters = $g_strings_utility_namespace->check_string_parameters(
				$this,$this->id,t_query_parameter::separator_level_0,
                $this->main_query_parameter->get_parameter_value(
					t_cuteengine_component::main_query_field_name_specific_parameters,-1),
				$this->get_specific_parameters_position_last(),'SPECIFIC PARAMETERS');   
			//print_r($this->specific_parameters);
			if ((count($this->specific_parameters)) < ($this->get_specific_parameters_position_last()-1))
			{	
				//echo "count: " . (count($this->specific_parameters)) ." - last: ". $this->get_specific_parameters_position_last() . "<br>";
				// add default values
				   for($local_index = (count($this->specific_parameters)+1); $local_index < $this->get_specific_parameters_position_last(); $local_index++)
				   {
						//echo "local_index: $local_index<br>";
						$this->specific_parameters[$local_index] = $this::get_default_specific_parameter($local_index);
						//echo "this->specific_parameters[$local_index]: ". $this->specific_parameters[$local_index]."<br>"; 
				   }
				   
			}				
               
		}		
		public function __call($name, $arguments) 
		{
			// included file
            $local_return = array();
			$local_file_name = get_class($this) . "_$name";
			$local_component_path = CUTEENGINE_COMPONENTS_PATH . substr(get_class($this),2,strlen(get_class($this))) . DIRECTORY_SEPARATOR  ;
			$local_part_path = CUTEENGINE_BASE_PART_PATH. substr(get_class($this),2,strlen(get_class($this))) . DIRECTORY_SEPARATOR  ;
			
			//echo "this->id: $this->id <br>";
			if (file_exists(CUTEENGINE_PATH_CUSTOM_FILES . $local_file_name . "_" . $this->id . ".php"))
				require(CUTEENGINE_PATH_CUSTOM_FILES . $local_file_name . "_" . $this->id . ".php");
			else if (file_exists("$local_component_path$local_file_name.php"))
				require("$local_component_path$local_file_name.php");
			else if (file_exists("$local_part_path$local_file_name.php"))
				require("$local_part_path$local_file_name.php");				
			else
            {
				//throw new Exception("Error calling method '$name' for object class '".get_class($this)."' id: ".$this->id."<br>");
                //$this->log->add_log_on_page("files '$local_file_name.php' and '$local_file_name" . "_" . $this->id . ".php' not found.");     
            }    
			//echo "local_return: $local_return <br>";	
			return $local_return;
		}		
        // Q
        public function query_field($a_query_repository_name,$a_query_field)
        {
            return $this->parent_cuteengine_component->query_repository->query_field(
                $a_query_repository_name,$a_query_field);
        }
        public function query_param($a_query_repository_name,$a_component_param)
        {
            return $this->parent_cuteengine_component->query_repository->query_param(
                $a_query_repository_name,$a_component_param);
        }
		// E
		public function execute_component_action()
		{
			$local_method_label = $this->url_method();
           // echo "local_method_label:$local_method_label<br>";
			//$this->log->add_log_on_page("local_method_label: $local_method_label");
            if (isset($local_method_label) && ($local_method_label != t_url_parameter::default_method_label))
            {
				
                //echo "local_method_label: $local_method_label";
                //echo "this->parent_cuteengine_component->main_url_methods_array[local_method_label]: ". 
                    //$this->parent_cuteengine_component->main_url_methods_array[$local_method_label];
                // checks if the input method exists
                if (isset($this->parent_cuteengine_component->main_url_methods_array[$local_method_label]))
                {
                    $this->log->add_log_on_page("metodo: $local_method_label - Trovato: SI");
					//echo "metodo: $local_method_label - Trovato: SI";
                    return $this->parent_cuteengine_component->main_url_methods_array[$local_method_label];
                }
                else 
                {
                    $this->log->add_log_on_page("metodo: $local_method_label - Trovato: NO");
					//$this->log->add_log_error("metodo: '$local_method_label' non trovato.");
                    return false;
                }
            }
            else if ($this->default_method)
			{
				return $this->default_method; 
			}
            else
            {
                $this->log->add_log_error("metodo specificato non trovato.");
                return false;
            }           
		}       
        //F
        public function finalize_creation_component()
        {   

        }
		
        // M
        public function main_query_field($a_query_field)
        {
            return $this->parent_cuteengine_component->main_query_field($a_query_field);
        }		
		// G
         public function get_childs_list()
		{
            global $g_cuteengine;
			$local_childs_array = $g_cuteengine->get_childs_list($this->id);
            foreach($local_childs_array as $local_child_component_id)
            {
                $local_component = $g_cuteengine->components[$local_child_component_id]
					[t_cuteengine::components_cuteengine_component];
				if (isset($local_component))	
					$local_childs_array = array_merge($local_childs_array,$local_component->page_component->get_childs_list());
            }
            return $local_childs_array;
            
		}
		public function get_main_query_parameter_value($a_component_param)
        {

            return $this->parent_cuteengine_component->get_main_query_parameter_value($a_component_param);
        }

		public function get_query($a_query_repository_name)
		{
            return $this->parent_cuteengine_component->query_repository->get_query($a_query_repository_name);
		}
        
		public function get_repository_item($a_query_repository_name)
		{
			return $this->parent_cuteengine_component->query_repository->get_repository_item($a_query_repository_name);
		}
		
		function get_specific_parameters_help($a_index)
		{	
			$local_return = '';
			for ($i=0; $i < $this->get_specific_parameters_position_last(); $i++)
			{
				if ($local_return == '')
					$local_return = $this::get_help_specific_parameter($i);
				else
					$local_return .= ", ".$this::get_help_specific_parameter($i);
				
			}
		}
		
		function get_default_value_specific_parameters($a_index)
		{	
			$local_return = '';
			for ($i=0; $i < $this->get_specific_parameters_position_last(); $i++)
			{
				if ($local_return == '')
					$local_return = $this::get_default_specific_parameter($i);
				else
					$local_return .= ", ".$this::get_default_specific_parameter($i);
				
			}
		}

		// N
		public function new_query($a_query_repository_name,$a_sql_query,$a_connection = null)
		{
			$local_class_name = get_class($this);
			//echo "local_class_name:$local_class_name<br>";
			$this->parent_cuteengine_component->query_repository->new_query($a_query_repository_name,$a_sql_query,$a_connection,$local_class_name);
			
			
		}		
		public function new_query_parameter($a_query_repository_name,$a_new_param_name,$a_query_field_name,$a_query_position_of_value)
		{
			$this->parent_cuteengine_component->query_repository->new_query_parameter($a_query_repository_name,$a_new_param_name,$a_query_field_name,$a_query_position_of_value);
		}
		public function new_url_argument($a_argument_name,$a_argument_position_of_value)
		{
			$this->parent_cuteengine_component->new_url_argument($a_argument_name,$a_argument_position_of_value);
        }    
		public function new_url_method($a_method_name,$a_method_label)
		{
        /*
                           It associates the name of the method(a_method_name) belonging to the current class to a label(a_method_label) which will be usable in the url parameter m(e.g.: m=create) in case you want to call this method
                  */
			$this->parent_cuteengine_component->new_url_method($a_method_name,$a_method_label);
        }		
        
        // U
        public function url_argument($a_url_argument)
        {
            return $this->parent_cuteengine_component->url_argument($a_url_argument);
        }
        
        public function url_method()
        {
            return $this->parent_cuteengine_component->url_method();
        }
	}
	
	interface i_base_component {
		static function get_default_specific_parameter($a_index);
		static function get_help_specific_parameter($a_index);
	}  
	
?>